package com.qf.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.utils.JsonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("findByPage")
    public JsonResultUtil findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "10") Integer pageSize) {

        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询所有用户
        List<User> userList = userService.findByPage();

        //获取分页对象
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        //设置返回类型及携带的参数
        JsonResultUtil jsonResultUtil = JsonResultUtil.success();
        jsonResultUtil.setCount(pageInfo.getTotal());
        jsonResultUtil.setData(pageInfo.getList());
        return jsonResultUtil;
    }
}
